package com.aaa.opengltest;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.FloatBuffer;

public class LogUtils {
    static String className;//类名
    static String methodName;//方法名
    static int lineNumber;//行数

    private LogUtils() {
        /* Protect from instantiations */
    }

    public static boolean isDebuggable() {
        return true;
        //        return BuildConfig.DEBUG;
    }

    private static String createLog(String log) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(methodName);
        buffer.append("(").append(className).append(":").append(lineNumber).append(")");
        buffer.append(log);
        return buffer.toString();
    }

    private static void getMethodNames(StackTraceElement[] sElements) {
        className = sElements[1].getFileName();
        methodName = sElements[1].getMethodName();
        lineNumber = sElements[1].getLineNumber();
    }


    public static void e(String message) {
        if (!isDebuggable())
            return;

        // Throwable instance must be created before any methods
        getMethodNames(new Throwable().getStackTrace());
        Log.e(className, createLog(message));
    }


    public static void i(String message) {
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.i(className, createLog(message));
    }

    public static void d(String message) {
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.d(className, createLog(message));
    }

    public static void v(String message) {
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.v(className, createLog(message));
    }

    public static void w(String message) {
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.w(className, createLog(message));
    }

    public static void wtf(String message) {
        if (!isDebuggable())
            return;

        getMethodNames(new Throwable().getStackTrace());
        Log.wtf(className, createLog(message));
    }

    public static void ls(String msg) {  //信息太长,分段打印
        //因为String的length是字符数量不是字节数量所以为了防止中文字符过多，把4*1024的MAX字节打印长度改为2048字符数
        int max_str_length = 2048;
        while (msg.length() > max_str_length) {
            LogUtils.d(msg.substring(0, max_str_length));
            msg = msg.substring(max_str_length);
        }
        //剩余部分
        LogUtils.d(msg);
    }

    public static void la(float[] t, int width) {  //信息太长,分段打印
        if (t == null || t.length == 0) {
            return;
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < t.length; i++) {
            sb.append(t[i]).append(",");
            if (i % width == 0) {
                LogUtils.d("i : " + (i / width) + " " + sb.toString());
                sb.setLength(0);
            }
        }
        if (sb.length() != 0) {
            LogUtils.d("i : " + ((t.length - 1) / width + 1) + " " + sb.toString());
        }

    }

    public static void lf(FloatBuffer t, int width) {  //信息太长,分段打印
        if (t == null || t.limit() == 0) {
            return;
        }

        StringBuffer sb = new StringBuffer();
        while(t.position()<t.limit()){
            sb.append(t.get()).append(",");
            if (t.position() % width == 0) {
                LogUtils.d("i : " + (t.position() / width) + " " + sb.toString());
                sb.setLength(0);
            }
        }

        if (sb.length() != 0) {
            LogUtils.d("i : " + ((t.position() - 1) / width + 1) + " " + sb.toString());
        }

    }

    public static void print(String msg) {  //信息太长,分段打印
        String name = Environment.getExternalStorageDirectory().getAbsolutePath() + "/log/" + System.nanoTime();
        FileOutputStream fos = null;
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/log/");
        if (!file.exists()) {
            file.mkdirs();
            try {
                file = new File(name);
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            fos = new FileOutputStream(name);
            fos.write(msg.getBytes());
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
